<?php
namespace app\api\controller\xiluedu;

use app\common\controller\Api;
use app\common\model\xiluedu\TopicForum;
use app\common\model\xiluedu\UserCollection;
use app\common\model\xiluedu\UserFavourite;
use app\common\model\xiluedu\UserFocus;
use think\Exception;
use function fast\array_get;

class Forum extends Api
{
    protected $noNeedLogin = ['focus_lists','hot_lists','lists','detail'];
    protected $noNeedRight = ['*'];


    /**
     * 新增帖子
     */
    public function add_forum(){
        $params = $this->request->param('');
        try {
            $ret = TopicForum::add($params,$this->auth->id);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('查询成功',$ret);
    }

    /**
     * 关注用户帖子列表
     */
    public function focus_lists(){
        $param = $this->request->param('');
        $user_ids = $this->auth->isLogin() ? UserFocus::getUserIds() : [0];
        $result = TopicForum::lists($param,false,$user_ids,$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 我的帖子列表
     */
    public function my_lists(){
        $param = $this->request->param('');
        $user_ids = $this->auth->isLogin() ? [$this->auth->id] : [0];
        $result = TopicForum::lists($param,false,$user_ids,$this->auth->id,'my_forum');
        $this->success('查询成功', $result);
    }

    /**
     * 我的帖子删除
     */
    public function del_forum(){
        $params = $this->request->param('');
        try {
            $result = TopicForum::delForum($params,$this->auth->id);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

        $this->success('删除成功');
    }

    /**
     * 热门帖子
     */
    public function hot_lists(){
        $param = $this->request->param('');
        $result = TopicForum::lists($param,true,[],$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 帖子列表
     */
    public function lists(){
        $param = $this->request->param('');
        $result = TopicForum::lists($param,false,[],$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 帖子详情
     */
    public function detail(){
        $params = $this->request->param('');
        $forum_id = array_get($params,'forum_id');
        if(!$forum_id){
            $this->error('非法参数');
        }
        $result = TopicForum::detail($forum_id,$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * @ApiTitile (收藏&取消收藏)
     * @ApiSummary (收藏&取消收藏)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.forum/toggle_collection)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     * @ApiParams (name=forum_id, type=int, require=true, description="id")
     */
    public function toggle_collection() {
        $forum_id = $this->request->post('forum_id');
        $result = UserCollection::toggleCollection($this->auth->id, $forum_id, UserCollection::TYPE_FORUM);
        $this->success('操作成功',['is_collection'=>$result]);
    }

    /**
     * @ApiTitile (点赞&取消点赞)
     * @ApiSummary (点赞&取消点赞)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xiluedu.forum/toggle_fav)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     * @ApiParams (name=forum_id, type=int, require=true, description="id")
     */
    public function toggle_fav() {
        $forum_id = $this->request->post('forum_id');
        $result = UserFavourite::toggleFav($this->auth->id, $forum_id, UserFavourite::TYPE_FORUM);
        $this->success('操作成功',['is_fav_count'=>$result?1:0]);
    }

    /**
     * 关注和取关
     */
    public function toggle_focus(){
        $user_id = $this->request->post('user_id');
        if($user_id == $this->auth->id){
            $this->error('不可关注自己');
        }
        $result = UserFocus::toggleFocus($this->auth->id, $user_id);
        $this->success('操作成功',['is_focus_count'=>$result ? 1 : 0]);
    }

}